package liyan.shi.springboot.component;

import org.springframework.boot.autoconfigure.web.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;

import java.util.Map;

//给容器中注入自定义错误属性
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {

    //返回的map就是页面和json能获取的所有数据
    @Override
    public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
        Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
        errorAttributes.put("pers", "shily");
        //从请求中获取异常处理器中自定义的异常信息ext
        Map<String, Object> exceptionMap = (Map<String, Object>) requestAttributes.getAttribute("ext", 0);
        errorAttributes.put("ext", exceptionMap);

        return errorAttributes;
    }

}
